<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/ficon.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/icon.png">
  <link rel="mask-icon" href="/images/icon.png" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-minimal.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"bqlin.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":true,"comments":{"style":"tabs","active":"gitalk","storage":true,"lazyload":false,"nav":{"gitalk":{"order":-2}},"activeClass":"gitalk"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":-1,"unescape":false,"preload":false},"motion":{"enable":false,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="人耳听觉范围">
<meta property="og:type" content="article">
<meta property="og:title" content="音频基础技术">
<meta property="og:url" content="https://bqlin.github.io/posts/audio_basic_technology/index.html">
<meta property="og:site_name" content="權咚领域">
<meta property="og:description" content="人耳听觉范围">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/bqlin/image-land/raw/master/1615773041050-3cf23e1f-cd52-469b-b648-de0888eeed57.png">
<meta property="og:image" content="https://gitee.com/bqlin/image-land/raw/master/1615773052644-80b40cf3-456c-44e8-955c-f25777c8246f.png">
<meta property="og:image" content="https://gitee.com/bqlin/image-land/raw/master/1615773063221-2e1a0503-20ab-4284-bf61-961677beadf7.png">
<meta property="og:image" content="https://gitee.com/bqlin/image-land/raw/master/1615773071671-80a1ca9f-4f41-4b11-990e-ef7710cf2120.png">
<meta property="og:image" content="https://gitee.com/bqlin/image-land/raw/master/1615773078659-0b7012e9-17a0-41a8-bffa-7838d70324f9.png">
<meta property="og:image" content="https://gitee.com/bqlin/image-land/raw/master/1615773098988-be49753b-73f4-4d8a-a59e-8694a89767f0.png">
<meta property="og:image" content="https://gitee.com/bqlin/image-land/raw/master/1615773110640-28723744-dc39-4a10-ac20-8e83a91f9c05.png">
<meta property="article:published_time" content="2021-03-14T22:34:44.000Z">
<meta property="article:modified_time" content="2021-10-28T04:26:38.000Z">
<meta property="article:author" content="權咚">
<meta property="article:tag" content="音视频">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/bqlin/image-land/raw/master/1615773041050-3cf23e1f-cd52-469b-b648-de0888eeed57.png">

<link rel="canonical" href="https://bqlin.github.io/posts/audio_basic_technology/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>音频基础技术 | 權咚领域</title>
  
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-105338942-1"></script>
    <script>
      if (CONFIG.hostname === location.hostname) {
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', 'UA-105338942-1');
      }
    </script>


  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?98fcf61fd269408d07dcbc1e49311f9a";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="權咚领域" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">權咚领域</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Making app a work of art.</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://bqlin.github.io/posts/audio_basic_technology/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/icon.png">
      <meta itemprop="name" content="權咚">
      <meta itemprop="description" content="🤘🤯👾">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="權咚领域">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          音频基础技术
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-15 06:34:44" itemprop="dateCreated datePublished" datetime="2021-03-15T06:34:44+08:00">2021-03-15</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-28 12:26:38" itemprop="dateModified" datetime="2021-10-28T12:26:38+08:00">2021-10-28</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E9%9F%B3%E8%A7%86%E9%A2%91%E6%A6%82%E5%BF%B5/" itemprop="url" rel="index"><span itemprop="name">音视频概念</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="人耳听觉范围">人耳听觉范围</h2>
<p><img src="https://gitee.com/bqlin/image-land/raw/master/1615773041050-3cf23e1f-cd52-469b-b648-de0888eeed57.png" /></p>
<p>次声波，听觉范围：20Hz~20kHz，超声波</p>
<p>Hz，赫兹，一秒内振动的次数。</p>
<p>人发声范围：85Hz~1100Hz</p>
<h2 id="声音三要素">声音三要素</h2>
<ul>
<li>音调（频率）：音频的快慢。男生 &gt; 女生 &gt; 儿童</li>
<li>音量（强度）：振动的幅度</li>
<li>音色：谐波</li>
</ul>
<h3 id="音调频率">音调（频率）</h3>
<p>在一个波中，周期是完成一个周期所需的时间，频率是周期的倒数，以赫兹表示每秒周期数。从本质上说，完成一个周期所需的时间越短，频率越高；从视觉上看，峰值彼此靠近的波比峰值远的波具有更高的频率。虽然频率描述了波形循环重复率的数值度量，单音调更像是我们用来描述声音的主观术语。</p>
<h3 id="音量强度">音量（强度）</h3>
<p>强度是理解声音成分的另一个维度。声音强度描述了声音在一个区域内位移的声功率，以瓦特/平方米为单位。声音的功率是声音在某个单位时间内传递能量的速率，即强度本质上是声音置换的能量。</p>
<p>听者的持续时间、频率和年龄等混杂因素会影响声音的响度。</p>
<h3 id="音色">音色</h3>
<p>银色描述了赋予声音特征的多种属性。</p>
<h2 id="模拟信号数字化过程">模拟信号数字化过程</h2>
<p>模拟音频信号转化为数字音频信号：模拟音频信号是一个在时间上和幅度上都连续的信号，它的数字化过程如下所述。</p>
<p>模拟信号数字化的结果产物是PCM或WAV文件。</p>
<h3 id="采样">采样</h3>
<p>在时间轴上对信号进行数字化。</p>
<p>按照固定的时间间隔抽取模拟信号的值，这样，采样后就可以使一个时间连续的信息波变为在时间上取值数目有限的离散信号。</p>
<p>采样过程决定采样率（sample rate）。</p>
<h3 id="量化">量化</h3>
<p>在幅度轴上对信号进行数字化。用有限个幅度值近似还原原来连续变化的幅度值，把模拟信号的连续幅度变为有限数量的有一定间隔的离散值。</p>
<p>量化过程决定位深/采样大小，这是通过采样格式（sample format）体现的。</p>
<h3 id="编码">编码</h3>
<p>用二进制数表示每个采样的量化值（十进制数）。</p>
<p>原始音频数据：</p>
<ul>
<li><p>PCM，脉冲编码调制。</p>
<ul>
<li>纯粹的音频数据，不带音频格式。所以PCM音频流的码率计算方式如上所述。</li>
</ul></li>
<li><p>WAV，在PCM上添加音频信息的头。</p>
<ul>
<li>但除了存储PCM原始数据，它还可以存储压缩数据。</li>
<li>如下图可见，WAV存储的音频格式就是量化的信息：采样大小、采样率、声道数。</li>
</ul></li>
</ul>
<p><img src="https://gitee.com/bqlin/image-land/raw/master/1615773052644-80b40cf3-456c-44e8-955c-f25777c8246f.png" /></p>
<p>上述的<strong>编码</strong>是模数转换过程中的编码成二进制文件的过程，注意与<strong>音频编码</strong>分开。一般所说的音频编码更多是指对音频的压缩。</p>
<h3 id="小结">小结</h3>
<p>音频由波形组成，包括不同频率和振幅的波的叠加。为了在数字媒体内表示这些波形，需要对波形进行采样，其<strong>采样率</strong>需要（至少）可以表示您要复制的最高频率的声音；同时还需要存储足够的<strong>位深</strong>，以表示声音样本中波形的适当振幅（响度和柔度）。</p>
<ul>
<li><p>位深/采样大小：一个采样用多少 bit 存放。能够表达的数值范围。使用8位、16位表达。</p>
<ul>
<li>位深影响给定音频样本的动态范围。位深越高，表示的振幅越精确。如果在同一音频样本内有很多响亮和柔和的声音，则需要更大的位深才能正确表示这些声音。</li>
<li>增高位深还会降低音频样本内的信噪比。CD 音乐音频使用 16 位的位深。DVD 音频使用 24 位的位深，而大多数电话设备使用 8 位的位深。（某些压缩技术可以补偿较小位深的不足，但往往会有损耗。）</li>
</ul></li>
<li><p>采样率：采样频率，即一秒内采样的个数。8k、16k、32k、44.1k、48k。越高越精细，高保真。</p>
<ul>
<li>声音以模拟波形的形式存在。数字音频片段以足够快的速率对模拟波的振幅进行采样，模仿波的固有频率，达到高度接近这种模拟波的效果。数字音频片段的采样率指定了（每秒）从音频的源素材中采集的样本数；采样率越高，数字音频如实表示高频的能力就越强。</li>
<li>根据 <a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Nyquist–Shannon_sampling_theorem">Nyquist-Shannon 定理</a>，对于您要以数字形式采集的任何声波，您的采样率通常需要高于其最高频率的两倍。例如，要表示人类听觉范围 (20-20000 Hz) 内的音频，数字音频格式必须至少每秒采样 40000 次（CD 音频使用 44100 Hz 的采样率，部分原因也在于此）。</li>
</ul></li>
<li><p>声道：单声道、双声道、多声道。</p></li>
</ul>
<p>通过以上三者可以计算出原始音频的码率（一秒内的比特数）：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">未经压缩的码率 = 采样率 × 采样大小 × 声道数</span><br><span class="line">44100 * 16 * 2 / 1000 = 1378.125kbps</span><br><span class="line">一分钟的存储空间：</span><br><span class="line">1378.125 * 60 / 8 / 1024 = 10.09MB</span><br></pre></td></tr></table></figure>
<h3 id="直接读取处理pcm">直接读取处理PCM</h3>
<p>对于PCM，其每个采样的都是固定的，可以直接通过指针进行访问操作，如把PCM16LE双声道（采样大小16位，即每个声道的采样大小为2字节；LE：使用小端方式存储）分离声道：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">FILE *fpcm = fopen(url, <span class="string">&quot;rb+&quot;</span>);</span><br><span class="line">FILE *fl = fopen(<span class="string">&quot;output_l.pcm&quot;</span>, <span class="string">&quot;wb+&quot;</span>);</span><br><span class="line">FILE *fr = fopen(<span class="string">&quot;output_r.pcm&quot;</span>, <span class="string">&quot;wb+&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 包含左右声道的采样</span></span><br><span class="line"><span class="type">unsigned</span> <span class="type">char</span> *sample = (<span class="type">unsigned</span> <span class="type">char</span> *)<span class="built_in">malloc</span>(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (!feof(fpcm)) &#123;</span><br><span class="line">    fread(sample, <span class="number">1</span>, <span class="number">4</span>, fpcm);</span><br><span class="line">    <span class="comment">// L</span></span><br><span class="line">    fwrite(sample, <span class="number">1</span>, <span class="number">2</span>, fl);</span><br><span class="line">    <span class="comment">// R</span></span><br><span class="line">    fwrite(sample + <span class="number">2</span>, <span class="number">1</span>, <span class="number">2</span>, fr);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">free</span>(sample);</span><br><span class="line">fclose(fpcm);</span><br><span class="line">fclose(fl);</span><br><span class="line">fclose(fr);</span><br></pre></td></tr></table></figure>
<p>类似的，将左声道音量降低一半：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">FILE *fin = fopen(url, <span class="string">&quot;rb+&quot;</span>);</span><br><span class="line">FILE *fout = fopen(<span class="string">&quot;output_halfleft.pcm&quot;</span>, <span class="string">&quot;wb+&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="type">unsigned</span> <span class="type">char</span> *sample = (<span class="type">unsigned</span> <span class="type">char</span> *)<span class="built_in">malloc</span>(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (!feof(fin)) &#123;</span><br><span class="line">    <span class="type">short</span> *samplel = <span class="literal">NULL</span>;</span><br><span class="line">    fread(sample, <span class="number">1</span>, <span class="number">4</span>, fin);</span><br><span class="line"></span><br><span class="line">    samplel = (<span class="type">short</span> *)sample;</span><br><span class="line">    *samplel = *samplel / <span class="number">2</span>;</span><br><span class="line">    fwrite(sample, <span class="number">1</span>, <span class="number">4</span>, fout);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">free</span>(sample);</span><br><span class="line">fclose(fin);</span><br><span class="line">fclose(fout);</span><br></pre></td></tr></table></figure>
<p>而加速，而可以采用隔位采样的方式实现，但这样的效果音调也会上去。例如把速度提升一倍：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">FILE *fin = fopen(url, <span class="string">&quot;rb+&quot;</span>);</span><br><span class="line">FILE *fout = fopen(<span class="string">&quot;output_doublespeed.pcm&quot;</span>, <span class="string">&quot;wb+&quot;</span>);</span><br><span class="line"><span class="type">int</span> cnt = <span class="number">0</span>;</span><br><span class="line"><span class="type">unsigned</span> <span class="type">char</span> *sample = (<span class="type">unsigned</span> <span class="type">char</span> *)<span class="built_in">malloc</span>(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (!feof(fin)) &#123;</span><br><span class="line">    fread(sample, <span class="number">1</span>, <span class="number">4</span>, fin);</span><br><span class="line">    <span class="keyword">if</span> (cnt % <span class="number">2</span> != <span class="number">0</span>) &#123; fwrite(sample, <span class="number">1</span>, <span class="number">4</span>, fout); &#125;</span><br><span class="line">    cnt++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">free</span>(sample);</span><br><span class="line">fclose(fin);</span><br><span class="line">fclose(fout);</span><br></pre></td></tr></table></figure>
<p>还可以进行采样格式的转换，例如简单把PCM16LE转换为PCM8，由于是降采样，所以音质也会下降。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">FILE *fin = fopen(url, <span class="string">&quot;rb+&quot;</span>);</span><br><span class="line">FILE *fout = fopen(<span class="string">&quot;output_8.pcm&quot;</span>, <span class="string">&quot;wb+&quot;</span>);</span><br><span class="line"><span class="type">int</span> cnt = <span class="number">0</span>;</span><br><span class="line"><span class="type">unsigned</span> <span class="type">char</span> *sample = (<span class="type">unsigned</span> <span class="type">char</span> *)<span class="built_in">malloc</span>(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (!feof(fin)) &#123;</span><br><span class="line">    <span class="type">short</span> *s16 = <span class="literal">NULL</span>;</span><br><span class="line">    <span class="type">char</span> s8 = <span class="number">0</span>;</span><br><span class="line">    <span class="type">unsigned</span> <span class="type">char</span> s8_u = <span class="number">0</span>;</span><br><span class="line">    fread(sample, <span class="number">1</span>, <span class="number">4</span>, fin);</span><br><span class="line">    <span class="comment">//(-32768-32767)</span></span><br><span class="line">    s16 = (<span class="type">short</span> *)sample;</span><br><span class="line">    s8 = (*s16) &gt;&gt; <span class="number">8</span>;</span><br><span class="line">    <span class="comment">//(0-255)</span></span><br><span class="line">    s8_u = s8 + <span class="number">128</span>;</span><br><span class="line">    <span class="comment">// L</span></span><br><span class="line">    fwrite(&amp;s8_u, <span class="number">1</span>, <span class="number">1</span>, fout);</span><br><span class="line"></span><br><span class="line">    s16 = (<span class="type">short</span> *)(sample + <span class="number">2</span>);</span><br><span class="line">    s8 = (*s16) &gt;&gt; <span class="number">8</span>;</span><br><span class="line">    s8_u = s8 + <span class="number">128</span>;</span><br><span class="line">    <span class="comment">// R</span></span><br><span class="line">    fwrite(&amp;s8_u, <span class="number">1</span>, <span class="number">1</span>, fout);</span><br><span class="line">    cnt++;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">free</span>(sample);</span><br><span class="line">fclose(fin);</span><br><span class="line">fclose(fout);</span><br></pre></td></tr></table></figure>
<h2 id="音频压缩编码">音频压缩编码</h2>
<p>与所有数据一样，音频数据通常会进行压缩，以便更易于存储和传输。音频编码中的压缩可能为无损或有损。无损压缩经过解包后可以将数字数据恢复为原始形式。有损压缩在压缩和解压缩过程中必然会移除某些信息，并且进行参数化，以便表明在多大容限范围内允许压缩技术移除数据。</p>
<p>音频压缩往往追求两个极端：压缩比尽可能大、压缩速度尽可能快。</p>
<h3 id="基本过程">基本过程</h3>
<p><img src="https://gitee.com/bqlin/image-land/raw/master/1615773063221-2e1a0503-20ab-4284-bf61-961677beadf7.png" /></p>
<p>以上过程包含了有损压缩和无损压缩的过程。主要还是有损压缩。</p>
<p>时域转频域：去除被遮蔽掉的音频信号；心理声学模型：去除人耳听觉范围以外的音频信号。</p>
<h3 id="压缩方式">压缩方式</h3>
<ul>
<li>有损压缩（清除后无法恢复）：消除冗余信息
<ul>
<li>在保证信号在听觉方面不产生失真的前提下，对音频数据信号尽可能大的压缩。这些冗余信息：
<ul>
<li>人耳听觉范围外的音频信号</li>
<li>被遮蔽掉的音频信号
<ul>
<li>频域遮蔽</li>
<li>时域遮蔽</li>
</ul></li>
</ul></li>
</ul></li>
<li>无损压缩</li>
</ul>
<h3 id="有损压缩编码">有损压缩编码</h3>
<p>有损压缩则会在构建压缩数据期间清除或减少某些类型的信息，从而压缩音频数据。</p>
<h4 id="频域遮蔽效应">频域遮蔽效应</h4>
<p><img src="https://gitee.com/bqlin/image-land/raw/master/1615773071671-80a1ca9f-4f41-4b11-990e-ef7710cf2120.png" /></p>
<p>音量高的会遮蔽附近音调的声音。</p>
<h4 id="时域遮蔽效应">时域遮蔽效应</h4>
<p><img src="https://gitee.com/bqlin/image-land/raw/master/1615773078659-0b7012e9-17a0-41a8-bffa-7838d70324f9.png" /></p>
<p>音量高的会遮蔽附近时间的声音。</p>
<h3 id="无损压缩编码">无损压缩编码</h3>
<p>无损压缩对存储的数据进行复杂的重排，从而压缩数字音频数据，但不会导致原始数字样本的质量下降。如果采用无损压缩，在将数据解包为原始数字形式时，不会丢失任何信息。</p>
<p>那么，无损压缩技术为什么有时会具有优化参数？这些参数通常用来控制文件大小和解压缩时间。例如，FLAC 使用 0（最快）到 8（文件大小最小）的压缩级别参数。与较低级别的压缩相比，较高级别的 FLAC 压缩不会丢失任何信息。压缩算法只是需要在构建或解构原始数字音频时消耗更多的计算能量。</p>
<p>从技术上讲，<code>LINEAR16</code> 不是“无损压缩”，因为首先它并未涉及压缩。</p>
<ul>
<li><p>熵编码</p>
<ul>
<li>哈夫曼编码。使用很小的二进制数代表一个较长的字符，频率越高编码越小，频率越低编码越长。</li>
</ul></li>
<li><p>算术编码</p>
<ul>
<li>通过二进制的小数来进行编码。</li>
</ul></li>
<li><p>香农编码</p>
<ul>
<li>算术编码的改进。</li>
</ul></li>
</ul>
<h2 id="常见音频编码器">常见音频编码器</h2>
<p>常见的音频编码器包括 OPUS、AAC、Ogg、Speex、iLBC、AMR、G.711 等。</p>
<ul>
<li><p>OPUS</p>
<ul>
<li>延迟小，压缩比高。WebRTC 默认使用。</li>
</ul></li>
<li><p>AAC</p>
<ul>
<li>应用广泛，移动设备支持硬编解码。用于取代 mp3。</li>
</ul></li>
<li><p>Ogg</p>
<ul>
<li>收费，因此导致应用不广。</li>
</ul></li>
<li><p>Speex</p>
<ul>
<li>直接支持回音消除功能。</li>
</ul></li>
<li><p>G.711</p>
<ul>
<li>窄带音频，编码后数据非常小，但声音损坏较大。固话。</li>
</ul></li>
</ul>
<p>网上评测结果：OPUS &gt; AAC &gt; Ogg</p>
<p><img src="https://gitee.com/bqlin/image-land/raw/master/1615773098988-be49753b-73f4-4d8a-a59e-8694a89767f0.png" /></p>
<p><img src="https://gitee.com/bqlin/image-land/raw/master/1615773110640-28723744-dc39-4a10-ac20-8e83a91f9c05.png" /></p>
<h2 id="音频重采样">音频重采样</h2>
<p>含义：将音频三元组（采样率、采样大小、通道数）的值转换成另外一组值。即只要改变这三元组的任意值都是进行重采样。</p>
<p>重采样的应用场景：</p>
<ul>
<li><p>从设备采集的音频数据与编码器要求的数据不一致。</p></li>
<li><p>输出设备要求的音频数据与要播放的音频数据不一致。</p></li>
<li><p>更方便计算，如回音消除要把双声道转换成单声道。</p></li>
</ul>
<h2 id="码控">码控</h2>
<ul>
<li>In CBR (constant bit rate) formats, such as linear PCM and IMA/ADPCM, all packets are the same size.</li>
<li>In VBR (variable bit rate) formats, such as AAC, Apple Lossless, and MP3, all packets have the same number of frames but the number of bits in each sample value can vary.</li>
<li>In VFR (variable frame rate) formats, packets have a varying number of frames. There are no commonly used formats of this type.</li>
</ul>

    </div>

    
    
    
        

  <div class="followme">
    <p>欢迎关注我的其它发布渠道</p>

    <div class="social-list">

        <div class="social-item">
          <a target="_blank" class="social-link" href="/atom.xml">
            <span class="icon">
              <i class="fa fa-rss"></i>
            </span>

            <span class="label">RSS</span>
          </a>
        </div>
    </div>
  </div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E9%9F%B3%E8%A7%86%E9%A2%91/" rel="tag"># 音视频</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/flv/" rel="prev" title="FLV格式">
      <i class="fa fa-chevron-left"></i> FLV格式
    </a></div>
      <div class="post-nav-item">
    <a href="/posts/common_audio_formats/" rel="next" title="常见音频格式">
      常见音频格式 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%BA%E8%80%B3%E5%90%AC%E8%A7%89%E8%8C%83%E5%9B%B4"><span class="nav-text">人耳听觉范围</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A3%B0%E9%9F%B3%E4%B8%89%E8%A6%81%E7%B4%A0"><span class="nav-text">声音三要素</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9F%B3%E8%B0%83%E9%A2%91%E7%8E%87"><span class="nav-text">音调（频率）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9F%B3%E9%87%8F%E5%BC%BA%E5%BA%A6"><span class="nav-text">音量（强度）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9F%B3%E8%89%B2"><span class="nav-text">音色</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%A8%A1%E6%8B%9F%E4%BF%A1%E5%8F%B7%E6%95%B0%E5%AD%97%E5%8C%96%E8%BF%87%E7%A8%8B"><span class="nav-text">模拟信号数字化过程</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%87%87%E6%A0%B7"><span class="nav-text">采样</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%87%8F%E5%8C%96"><span class="nav-text">量化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BC%96%E7%A0%81"><span class="nav-text">编码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93"><span class="nav-text">小结</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%B4%E6%8E%A5%E8%AF%BB%E5%8F%96%E5%A4%84%E7%90%86pcm"><span class="nav-text">直接读取处理PCM</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9F%B3%E9%A2%91%E5%8E%8B%E7%BC%A9%E7%BC%96%E7%A0%81"><span class="nav-text">音频压缩编码</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E8%BF%87%E7%A8%8B"><span class="nav-text">基本过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8E%8B%E7%BC%A9%E6%96%B9%E5%BC%8F"><span class="nav-text">压缩方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9C%89%E6%8D%9F%E5%8E%8B%E7%BC%A9%E7%BC%96%E7%A0%81"><span class="nav-text">有损压缩编码</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%91%E5%9F%9F%E9%81%AE%E8%94%BD%E6%95%88%E5%BA%94"><span class="nav-text">频域遮蔽效应</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%97%B6%E5%9F%9F%E9%81%AE%E8%94%BD%E6%95%88%E5%BA%94"><span class="nav-text">时域遮蔽效应</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%97%A0%E6%8D%9F%E5%8E%8B%E7%BC%A9%E7%BC%96%E7%A0%81"><span class="nav-text">无损压缩编码</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%B8%E8%A7%81%E9%9F%B3%E9%A2%91%E7%BC%96%E7%A0%81%E5%99%A8"><span class="nav-text">常见音频编码器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9F%B3%E9%A2%91%E9%87%8D%E9%87%87%E6%A0%B7"><span class="nav-text">音频重采样</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A0%81%E6%8E%A7"><span class="nav-text">码控</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="權咚"
      src="/images/icon.png">
  <p class="site-author-name" itemprop="name">權咚</p>
  <div class="site-description" itemprop="description">🤘🤯👾</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">84</span>
          <span class="site-state-item-name">文章</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/bqlin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;bqlin" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/hearingdog" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;hearingdog" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i>Weibo</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://xiaozhuanlan.com/metal-reference-translation" title="https:&#x2F;&#x2F;xiaozhuanlan.com&#x2F;metal-reference-translation" rel="noopener" target="_blank">Metal内参</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://xiaozhuanlan.com/wwdc21" title="https:&#x2F;&#x2F;xiaozhuanlan.com&#x2F;wwdc21" rel="noopener" target="_blank">WWDC21 内参</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2016 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">權咚</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">279k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">4:14</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/pangu@4/dist/browser/pangu.min.js"></script>

<script src="/js/utils.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'default',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
          load: ['[tex]/mhchem'],
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
          packages: {'[+]': ['mhchem']},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : '7ed324dbfd2741c35c0e',
      clientSecret: '75a59e1f6a55202c4a1b0ec3570ad5b5dccbc390',
      repo        : 'bqlin.github.io',
      owner       : 'bqlin',
      admin       : ['bqlin'],
      id          : 'e21d1f2b5da2d0a720531b5c2a836e3a',
        language: 'zh-CN',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","vOffset":-100},"mobile":{"show":false,"scale":0.5},"log":false});</script></body>
</html>
